package device

import (
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
)

// ==================== 银联码查询 ====================

// UnionPayCodeQueryRequest 银联码查询请求
type UnionPayCodeQueryRequest struct {
	models.OrgBaseRequest
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewUnionPayCodeQueryRequest 创建银联码查询请求
func NewUnionPayCodeQueryRequest() *UnionPayCodeQueryRequest {
	req := &UnionPayCodeQueryRequest{}
	return req
}

// SetMerchNo 设置商户号
func (r *UnionPayCodeQueryRequest) SetMerchNo(merchNo string) *UnionPayCodeQueryRequest {
	r.MerchNo = merchNo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *UnionPayCodeQueryRequest) SetRawRequestMap(rawMap map[string]interface{}) *UnionPayCodeQueryRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *UnionPayCodeQueryRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *UnionPayCodeQueryRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MerchNo != "" {
		baseMap["merch_no"] = r.MerchNo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *UnionPayCodeQueryRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// UnionPayCodeQueryResponse 银联码查询响应
type UnionPayCodeQueryResponse struct {
	models.StandardResponse
	// Qrcodes 二维码列表
	Qrcodes []map[string]interface{} `json:"qrcodes,omitempty"`
}

// NewUnionPayCodeQueryResponse 创建银联码查询响应
func NewUnionPayCodeQueryResponse() *UnionPayCodeQueryResponse {
	return &UnionPayCodeQueryResponse{}
}

// GetQrcodeList 获取二维码列表
func (r *UnionPayCodeQueryResponse) GetQrcodeList() []map[string]interface{} {
	if r.Qrcodes == nil {
		return []map[string]interface{}{}
	}
	return r.Qrcodes
}

// GetFirstQrcode 获取第一个二维码信息
func (r *UnionPayCodeQueryResponse) GetFirstQrcode() map[string]interface{} {
	if r.Qrcodes != nil && len(r.Qrcodes) > 0 {
		return r.Qrcodes[0]
	}
	return nil
}

// GetQrcodeByTermNo 根据终端号获取二维码信息
func (r *UnionPayCodeQueryResponse) GetQrcodeByTermNo(termNo string) map[string]interface{} {
	if r.Qrcodes != nil {
		for _, qrcode := range r.Qrcodes {
			if qrcode["term_no"] == termNo {
				return qrcode
			}
		}
	}
	return nil
}

// ==================== 银联码加机 ====================

// UnionPayCodeAddRequest 银联码加机请求
type UnionPayCodeAddRequest struct {
	models.OrgBaseRequest
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// PrintMerchName 打印商户名称
	PrintMerchName string `json:"print_merch_name,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewUnionPayCodeAddRequest 创建银联码加机请求
func NewUnionPayCodeAddRequest() *UnionPayCodeAddRequest {
	req := &UnionPayCodeAddRequest{}
	return req
}

// SetMerchNo 设置商户号
func (r *UnionPayCodeAddRequest) SetMerchNo(merchNo string) *UnionPayCodeAddRequest {
	r.MerchNo = merchNo
	return r
}

// SetPrintMerchName 设置打印商户名称
func (r *UnionPayCodeAddRequest) SetPrintMerchName(printMerchName string) *UnionPayCodeAddRequest {
	r.PrintMerchName = printMerchName
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *UnionPayCodeAddRequest) SetRawRequestMap(rawMap map[string]interface{}) *UnionPayCodeAddRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *UnionPayCodeAddRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *UnionPayCodeAddRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MerchNo != "" {
		baseMap["merch_no"] = r.MerchNo
	}
	if r.PrintMerchName != "" {
		baseMap["print_merch_name"] = r.PrintMerchName
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *UnionPayCodeAddRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// UnionPayCodeAddResponse 银联码加机响应
type UnionPayCodeAddResponse struct {
	models.StandardResponse
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// QrcodeURL 二维码URL
	QrcodeURL string `json:"qrcode_url,omitempty"`
}

// NewUnionPayCodeAddResponse 创建银联码加机响应
func NewUnionPayCodeAddResponse() *UnionPayCodeAddResponse {
	return &UnionPayCodeAddResponse{}
}

// ==================== 设备变更 ====================

// DeviceChangeRequest 设备变更请求
type DeviceChangeRequest struct {
	models.OrgBaseRequest
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// NewDeviceNo 新设备号
	NewDeviceNo string `json:"new_device_no,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewDeviceChangeRequest 创建设备变更请求
func NewDeviceChangeRequest() *DeviceChangeRequest {
	req := &DeviceChangeRequest{}
	return req
}

// SetMerchNo 设置商户号
func (r *DeviceChangeRequest) SetMerchNo(merchNo string) *DeviceChangeRequest {
	r.MerchNo = merchNo
	return r
}

// SetTermNo 设置终端号
func (r *DeviceChangeRequest) SetTermNo(termNo string) *DeviceChangeRequest {
	r.TermNo = termNo
	return r
}

// SetNewDeviceNo 设置新设备号
func (r *DeviceChangeRequest) SetNewDeviceNo(newDeviceNo string) *DeviceChangeRequest {
	r.NewDeviceNo = newDeviceNo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *DeviceChangeRequest) SetRawRequestMap(rawMap map[string]interface{}) *DeviceChangeRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *DeviceChangeRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *DeviceChangeRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MerchNo != "" {
		baseMap["merch_no"] = r.MerchNo
	}
	if r.TermNo != "" {
		baseMap["term_no"] = r.TermNo
	}
	if r.NewDeviceNo != "" {
		baseMap["new_device_no"] = r.NewDeviceNo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *DeviceChangeRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// DeviceChangeResponse 设备变更响应
type DeviceChangeResponse struct {
	models.StandardResponse
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// DeviceNo 设备号
	DeviceNo string `json:"device_no,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
}

// NewDeviceChangeResponse 创建设备变更响应
func NewDeviceChangeResponse() *DeviceChangeResponse {
	return &DeviceChangeResponse{}
}

// ==================== 设备撤销 ====================

// DeviceCancelRequest 设备撤销请求
type DeviceCancelRequest struct {
	models.OrgBaseRequest
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewDeviceCancelRequest 创建设备撤销请求
func NewDeviceCancelRequest() *DeviceCancelRequest {
	req := &DeviceCancelRequest{}
	return req
}

// SetMerchNo 设置商户号
func (r *DeviceCancelRequest) SetMerchNo(merchNo string) *DeviceCancelRequest {
	r.MerchNo = merchNo
	return r
}

// SetTermNo 设置终端号
func (r *DeviceCancelRequest) SetTermNo(termNo string) *DeviceCancelRequest {
	r.TermNo = termNo
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *DeviceCancelRequest) SetRawRequestMap(rawMap map[string]interface{}) *DeviceCancelRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *DeviceCancelRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *DeviceCancelRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MerchNo != "" {
		baseMap["merch_no"] = r.MerchNo
	}
	if r.TermNo != "" {
		baseMap["term_no"] = r.TermNo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *DeviceCancelRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// DeviceCancelResponse 设备撤销响应
type DeviceCancelResponse struct {
	models.StandardResponse
	// DeviceNo 设备号
	DeviceNo string `json:"device_no,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
}

// NewDeviceCancelResponse 创建设备撤销响应
func NewDeviceCancelResponse() *DeviceCancelResponse {
	return &DeviceCancelResponse{}
}

// ==================== 设备添加 ====================

// DeviceAddRequest 设备添加请求
type DeviceAddRequest struct {
	models.OrgBaseRequest
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// DeviceType 设备类型
	DeviceType string `json:"device_type,omitempty"`
	// DeviceNo 设备号
	DeviceNo string `json:"device_no,omitempty"`
	// PrintMerchName 打印商户名称
	PrintMerchName string `json:"print_merch_name,omitempty"`
	// DetAddress 设备地址
	DetAddress string `json:"det_address,omitempty"`
	// ShopID 门店ID
	ShopID string `json:"shop_id,omitempty"`
	// 原始请求映射数据
	rawRequestMap map[string]interface{} `json:"-"`
}

// NewDeviceAddRequest 创建设备添加请求
func NewDeviceAddRequest() *DeviceAddRequest {
	req := &DeviceAddRequest{}
	return req
}

// SetMerchNo 设置商户号
func (r *DeviceAddRequest) SetMerchNo(merchNo string) *DeviceAddRequest {
	r.MerchNo = merchNo
	return r
}

// SetDeviceType 设置设备类型
func (r *DeviceAddRequest) SetDeviceType(deviceType string) *DeviceAddRequest {
	r.DeviceType = deviceType
	return r
}

// SetDeviceNo 设置设备号
func (r *DeviceAddRequest) SetDeviceNo(deviceNo string) *DeviceAddRequest {
	r.DeviceNo = deviceNo
	return r
}

// SetPrintMerchName 设置打印商户名称
func (r *DeviceAddRequest) SetPrintMerchName(printMerchName string) *DeviceAddRequest {
	r.PrintMerchName = printMerchName
	return r
}

// SetDetAddress 设置设备地址
func (r *DeviceAddRequest) SetDetAddress(detAddress string) *DeviceAddRequest {
	r.DetAddress = detAddress
	return r
}

// SetShopID 设置门店ID
func (r *DeviceAddRequest) SetShopID(shopID string) *DeviceAddRequest {
	r.ShopID = shopID
	return r
}

// SetRawRequestMap 设置原始请求映射数据
func (r *DeviceAddRequest) SetRawRequestMap(rawMap map[string]interface{}) *DeviceAddRequest {
	r.rawRequestMap = rawMap
	return r
}

// GetRawMap 获取原始请求映射数据
func (r *DeviceAddRequest) GetRawMap() (map[string]interface{}, bool) {
	return r.rawRequestMap, len(r.rawRequestMap) > 0
}

// ToMap 转换为map
func (r *DeviceAddRequest) ToMap() (map[string]interface{}, error) {
	// 如果有原始请求数据，优先使用
	if rawMap, hasRaw := r.GetRawMap(); hasRaw {
		return rawMap, nil
	}

	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MerchNo != "" {
		baseMap["merch_no"] = r.MerchNo
	}
	if r.DeviceType != "" {
		baseMap["device_type"] = r.DeviceType
	}
	if r.DeviceNo != "" {
		baseMap["device_no"] = r.DeviceNo
	}
	if r.PrintMerchName != "" {
		baseMap["print_merch_name"] = r.PrintMerchName
	}
	if r.DetAddress != "" {
		baseMap["det_address"] = r.DetAddress
	}
	if r.ShopID != "" {
		baseMap["shop_id"] = r.ShopID
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *DeviceAddRequest) CheckRequiredParams(config *core.Config) error {
	// 与Python版本保持一致，不进行参数校验
	return nil
}

// DeviceAddResponse 设备添加响应
type DeviceAddResponse struct {
	models.StandardResponse
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// DeviceNo 设备号
	DeviceNo string `json:"device_no,omitempty"`
	// TermNo 终端号
	TermNo string `json:"term_no,omitempty"`
}

// NewDeviceAddResponse 创建设备添加响应
func NewDeviceAddResponse() *DeviceAddResponse {
	return &DeviceAddResponse{}
}

// ==================== 设备查询 ====================

// DeviceInfo 设备信息
//
// @author liaochangyi
// @since 2025-01-25
type DeviceInfo struct {
	// DeviceNo 设备硬件序列号（SN）
	DeviceNo string `json:"device_no,omitempty"`
	// TermNo 终端编号
	TermNo string `json:"term_no,omitempty"`
	// MerchNo 商户号
	MerchNo string `json:"merch_no,omitempty"`
	// Status 状态：0-待启用 1-启用 2-停用 3-注销
	Status string `json:"status,omitempty"`
	// Model 设备型号
	Model string `json:"model,omitempty"`
	// DeviceType 设备类型：02-传统POS 04-智能POS 05-智能POS AD-音响 CD-收银台 EP-电子码牌 PT-打印机 QR-码付设备 SP-立牌
	DeviceType string `json:"device_type,omitempty"`
	// Supplier 供应商
	Supplier string `json:"supplier,omitempty"`
	// MerchName 商户名称
	MerchName string `json:"merch_name,omitempty"`
	// PrintMerchName 打印商户名称，签购单上的商户名称
	PrintMerchName string `json:"print_merch_name,omitempty"`
	// Address 地址
	Address string `json:"address,omitempty"`
	// InstallTime 加机时间，yyyy-mm-dd HH:mm:ss
	InstallTime string `json:"install_time,omitempty"`
	// UpdateTime 更新时间，yyyy-mm-dd HH:mm:ss
	UpdateTime string `json:"update_time,omitempty"`
}

// NewDeviceInfo 创建设备信息
func NewDeviceInfo() *DeviceInfo {
	return &DeviceInfo{}
}

// DeviceQueryRequest 终端查询请求
//
// @author liaochangyi
// @since 2025-01-25
type DeviceQueryRequest struct {
	models.OrgBaseRequest
	// MerchNo 商户号，终端号、机身号不传查该商户下所有的终端信息
	MerchNo string `json:"merch_no,omitempty"`
	// TermNo 终端编号，填入终端号时查询该终端号的终端信息，终端号必须为该商户号下的终端
	TermNo string `json:"term_no,omitempty"`
	// DeviceNo 设备硬件序列号（SN），填入机身号时查询该设备的终端信息，机身号必须该商户号下加机绑定的设备对应的机身号
	DeviceNo string `json:"device_no,omitempty"`
}

// NewDeviceQueryRequest 创建终端查询请求
func NewDeviceQueryRequest() *DeviceQueryRequest {
	return &DeviceQueryRequest{}
}

// SetMerchNo 设置商户号
func (r *DeviceQueryRequest) SetMerchNo(merchNo string) *DeviceQueryRequest {
	r.MerchNo = merchNo
	return r
}

// SetTermNo 设置终端编号
func (r *DeviceQueryRequest) SetTermNo(termNo string) *DeviceQueryRequest {
	r.TermNo = termNo
	return r
}

// SetDeviceNo 设置设备硬件序列号
func (r *DeviceQueryRequest) SetDeviceNo(deviceNo string) *DeviceQueryRequest {
	r.DeviceNo = deviceNo
	return r
}

// ToMap 转换为map
func (r *DeviceQueryRequest) ToMap() (map[string]interface{}, error) {
	// 先获取基类的map
	baseMap, err := r.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加自己的字段
	if r.MerchNo != "" {
		baseMap["merch_no"] = r.MerchNo
	}
	if r.TermNo != "" {
		baseMap["term_no"] = r.TermNo
	}
	if r.DeviceNo != "" {
		baseMap["device_no"] = r.DeviceNo
	}

	return baseMap, nil
}

// CheckRequiredParams 检查必需参数
func (r *DeviceQueryRequest) CheckRequiredParams(config *core.Config) error {
	return r.OrgBaseRequest.CheckRequiredParams(config)
}

// GetApiUrl 获取API URL
func (r *DeviceQueryRequest) GetApiUrl() string {
	return "/open/merch/access/device/query"
}

// DeviceQueryResponse 终端查询响应
type DeviceQueryResponse struct {
	models.StandardResponse
	// Devices 设备列表
	Devices []*DeviceInfo `json:"devices,omitempty"`
}

// NewDeviceQueryResponse 创建终端查询响应
func NewDeviceQueryResponse() *DeviceQueryResponse {
	return &DeviceQueryResponse{}
}
